/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [CalculatorPanel.java]
 * Creation Date        [10-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_gui.calculator_V1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * 计算器的界面
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class CalculatorPanel extends JPanel {

	private static final long serialVersionUID = 3893661964387147428L;

	// 计算器上数字和符号键
	private final String[] NUMBER = { "7", "8", "9", "/", "sqrt", "4", "5",
			"6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
	private final JButton[] NUMBER_BUTTONS = new JButton[NUMBER.length];

	// 计算器上的命令键
	private final String[] COMMAND = { "Backspace", "CE", "C" };
	private final JButton[] COMMAND_BUTTONS = new JButton[COMMAND.length];

	// 键盘上的M键
	private final String[] MKEY = { " ", "MC", "MR", "MS", "M+" };
	private final JButton[] MKEY_BUTTONS = new JButton[MKEY.length];

	private ActionListener listener = null;

	/**
	 * 初始化计算器界面
	 */
	private void init() {

		// 初始化数字键和符号键面板
		JPanel numPanel = new JPanel();
		numPanel.setLayout(new GridLayout(4, 5, 3, 3));
		assembleButton(numPanel, NUMBER, NUMBER_BUTTONS, Color.BLUE);
		NUMBER_BUTTONS[3].setForeground(Color.RED);
		NUMBER_BUTTONS[8].setForeground(Color.RED);
		NUMBER_BUTTONS[13].setForeground(Color.RED);
		NUMBER_BUTTONS[18].setForeground(Color.RED);
		NUMBER_BUTTONS[19].setForeground(Color.RED);
		assembleListener(NUMBER_BUTTONS, listener);

		// 初始化命令键
		JPanel commandPanel = new JPanel();
		commandPanel.setLayout(new GridLayout(1, 3, 3, 3));
		assembleButton(commandPanel, COMMAND, COMMAND_BUTTONS, Color.RED);
		assembleListener(COMMAND_BUTTONS, listener);

		// 初始化M键
		JPanel mPanel = new JPanel();
		mPanel.setLayout(new GridLayout(5, 1, 3, 3));
		assembleButton(mPanel, MKEY, MKEY_BUTTONS, Color.RED);
		assembleListener(MKEY_BUTTONS, listener);

		// 放置数字键和命令键的按钮
		JPanel keyPanel = new JPanel();
		keyPanel.setLayout(new BorderLayout(3, 3));
		keyPanel.add(commandPanel, BorderLayout.NORTH);
		keyPanel.add(numPanel, BorderLayout.CENTER);

		// 计算器面板
		setLayout(new BorderLayout(3, 5));
		this.add("West", mPanel);
		this.add("Center", keyPanel);
	}

	/**
	 * 组装按钮
	 * 
	 * @param panel
	 * @param names
	 * @param buttons
	 * @param background
	 */
	private void assembleButton(JPanel panel, String[] names,
			JButton[] buttons, Color background) {
		for (int i = 0; i < names.length; i++) {
			buttons[i] = new JButton(names[i]);
			buttons[i].setForeground(background);
			panel.add(buttons[i]);
		}
	}

	/**
	 * 为按钮添加监听器
	 * 
	 * @param buttons
	 * @param listener
	 */
	public void assembleListener(JButton[] buttons, ActionListener listener) {
		for (int i = 0; i < buttons.length; i++) {
			buttons[i].addActionListener(listener);
		}
	}

	public CalculatorPanel(ActionListener listener) {
		this.listener = listener;
		init();
	}
}
